package cn.iocoder.yudao.module.apijson.controller.admin.access;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.apijson.controller.admin.access.vo.*;
import cn.iocoder.yudao.module.apijson.dal.dataobject.access.AccessDO;
import cn.iocoder.yudao.module.apijson.service.access.AccessService;

@Tag(name = "管理后台 - 权限配置(必须)")
@RestController
@RequestMapping("/apijson/access")
@Validated
public class AccessController {

    @Resource
    private AccessService accessService;

    @PostMapping("/create")
    @Operation(summary = "创建权限配置(必须)")
    @PreAuthorize("@ss.hasPermission('apijson:access:create')")
    public CommonResult<Long> createAccess(@Valid @RequestBody AccessSaveReqVO createReqVO) {
        return success(accessService.createAccess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新权限配置(必须)")
    @PreAuthorize("@ss.hasPermission('apijson:access:update')")
    public CommonResult<Boolean> updateAccess(@Valid @RequestBody AccessSaveReqVO updateReqVO) {
        accessService.updateAccess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除权限配置(必须)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('apijson:access:delete')")
    public CommonResult<Boolean> deleteAccess(@RequestParam("id") Long id) {
        accessService.deleteAccess(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除权限配置(必须)")
    @PreAuthorize("@ss.hasPermission('apijson:access:delete')")
    public CommonResult<Boolean> deleteAccessList(@RequestParam("ids") List<Long> ids) {
        accessService.deleteAccessListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得权限配置(必须)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('apijson:access:query')")
    public CommonResult<AccessRespVO> getAccess(@RequestParam("id") Long id) {
        AccessDO access = accessService.getAccess(id);
        return success(BeanUtils.toBean(access, AccessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得权限配置(必须)分页")
    @PreAuthorize("@ss.hasPermission('apijson:access:query')")
    public CommonResult<PageResult<AccessRespVO>> getAccessPage(@Valid AccessPageReqVO pageReqVO) {
        PageResult<AccessDO> pageResult = accessService.getAccessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出权限配置(必须) Excel")
    @PreAuthorize("@ss.hasPermission('apijson:access:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccessExcel(@Valid AccessPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccessDO> list = accessService.getAccessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "权限配置(必须).xls", "数据", AccessRespVO.class,
                BeanUtils.toBean(list, AccessRespVO.class));
    }

    @GetMapping("/getAllAccess")
    @Operation(summary = "获得权限配置(必须)列表")
    @PreAuthorize("@ss.hasPermission('apijson:access:getAllAccess')")
    public CommonResult<List<AccessAllDataRespVO>> getAllAccess(@Valid AccessAllDataReqVO reqVO) {
        List<AccessAllDataRespVO> allAccess = accessService.getAllAccess(reqVO);
        return success(allAccess);
    }

}